<?php

/**
 * @content: 获取体征数据
 * @Author:  Groube
 * @Date:    2017-03-08 15:30:00
 * @version  $Id$
 */
class GetTizhengListJsonController extends Yaf_Controller_Abstract {

    /**
     * @Title  禁用模板功能
     * @Author Groube
     * @Param  /
     * @Return /
     * @Date 2017/03/08 14:30
     */
    public function init() {
        $yiyuan_id = isset($_REQUEST['yiyuan_id']) ? $_REQUEST['yiyuan_id'] : "";

        if($yiyuan_id === "" ){
            Utility::apiJsonError(20028, '请输入医院ID');
        }elseif(!ctype_digit("$yiyuan_id")){
            Utility::apiJsonError(20027, '医院ID必须是整型');
        }

        Yaf_Dispatcher::getInstance()->disableView();
    }

    /**
     * @Title  入口方法
     * @Author Groube
     * @Param  /
     * @Return /
     * @Date 2017/03/08 14:20
     */
    function indexAction(){
        $request = $this->getRequest();
        $user_department = $request->getPost('user_department','');
        $user_department_id = $request->getPost('user_department_id','');
        $str_yiyuan_id = $request->getPost('yiyuan_id',0);
        $yiyuan_id = (int)$str_yiyuan_id;

        if(empty($user_department)){
            Utility::apiJsonError(40012,'(Method:获取体征数据)科室不能为空!');
        }

        $async_result = $this->getTizhengDataCommon($user_department_id,$user_department,$yiyuan_id);
        if(!$async_result){
            $data = array('error'=>200,'msg'=>'暂无数据!','update_sql' => '');
            Utility::apiJsonResultExtends($data);
        }

        $params_mod = new ZhuyuanTizhengModel;
        $field_arr = $params_mod->_field_arr;
        $async_result = Utility::formatToSqlStr($async_result,'zhuyuan_tizheng','update_sql',$field_arr);
        $async_result['error'] = 200;
        $async_result['msg'] = '';
        Utility::apiJsonResultExtends($async_result);
    }


    /**
     * 方法名：getTizhengDataCommon()
     * 功 能：获取体征数据
     * @param  $user_department
     * @param  $yiyuan_id
     * @return $async_result
     */
    public function getTizhengDataCommon($user_department_id,$user_department,$yiyuan_id){
        if(Yaconf::get("config$yiyuan_id.fenbiao_state") == 'true'){
            //分表机制开启
            $zhuyuan_tizheng = "zhuyuan_tizheng_".$user_department_id;
        }else{
            $zhuyuan_tizheng = "zhuyuan_tizheng";
        }

        $zhuyuan_basic_info = DB::select('zhuyuan_id')->from('zhuyuan_basic_info')
            ->where('zhuyuan_bingqu','=',$user_department)
            ->where('zhuangtai','=','住院中')
            ->where('yiyuan_id','=',$yiyuan_id)
            ->execute()
            ->as_array();

        $zhuayun_bingren = array();
        if(!empty($zhuyuan_basic_info)){
            foreach($zhuyuan_basic_info as $one) {
                if(!in_array($one['zhuyuan_id'],$zhuayun_bingren)){
                    $zhuayun_bingren[] = $one['zhuyuan_id'];
                }
            }
        }else{
            Utility::apiJsonError(40013,'(Method:获取体征数据)住院基本信息表中没有查到数据!');
        }

        $params_mod = new ZhuyuanTizhengModel;
        return $params_mod->ls($zhuyuan_tizheng,$zhuayun_bingren,$yiyuan_id);
    }
}